<?php
namespace Admin\Controller;

class ModelController extends AdminController{
    
    public function index($username='', $status=''){

        $where = 'usertype=0';
        $is_seller = 0;
        if(session('is_seller') == 1){ //是卖家
            $where .= ' and md_user.status=1';
            $is_seller = 1;
        }
        if(trim($username) != ''){
            $where .= ' and username like \'%' .$username . '%\'';
        }
        if($status != ''){
            $where .= ' and md_user.status='. $status;
        }
        $page = page('User', 10, $where, array('md_user.id' =>'desc'), 'md_user_model on md_user.id=md_user_model.uid', 
                'md_user.id, username, model_card, reg_time, md_user.status, realname, email, mobile, cardid, card_img, show_model_card, value');
        $page['is_seller'] = $is_seller;
        $this->assign($page);
        $this->display();
    }
    
    /**
     * 审核模特
     * @param int $id 模特id
     * @param int $status 状态：1-审核通过；2-审核未通过
     */
    public function checkModel($uid=0, $status=0){
        
        if(M('User')->save(array('id' => $uid, 'status' => $status))){
            M('User_model')->where(array('uid' => $uid))->save(array('status' => $status));
            if($status == 1){
                $msg = '恭喜您，已通过实名认证，想要月月赚翻 就要让我们的雇主更快的找到您，快去麻 豆卡秀出你的美照，提高你的曝光率。';
            }else{
                $msg = '对不起，您的实名认证审核未通过,由于 您身份证上的信息与发布照片不像符合， 请核实后重新编辑。';           
            }
            vendor('Api.ServerAPI');
            $p = new \ServerAPI(C('appKey'), C('appSecret'));
            $content = '{"content":"' .$msg. '","extra":"status_' .$status. '"}';
            $to = array($uid);
            $msg = $p->messageSystemPublish(2, $to, 'RC:TxtMsg', $content);
            $this->ajaxReturn(array('code' => 1));
        }
        $this->ajaxReturn(array('code' => 0));
    }
    
    /**
     * 删除模特（模特、微博）
     * @param int $id 模特id
     */
    public function delModel($id=0){
        M('User')->delete($id);
        M('User_model')->where(array('uid' => $id))->delete();
        $weibo = M('Weibo')->where(array('uid' => $id))->select();
        foreach ($weibo as $key => $value){
            $pics = M('Weibo_pic')->where(array('w_id' => $value['id']))->select();
            foreach ($pics as $k => $v) {
                unlink('.' . $v['pic_url']);
            }
        }
        M('Weibo')->where(array('uid' => $id))->delete();
        $this->redirect('Admin/Model/index');
    }

    /**
     * 模特卡列表
     * @param type $username 用户名
     * @param type $status   模特卡审核状态审核状态
     */
    public function modelCardList($username='', $status=''){

        $where = 'usertype=0 and model_card != "" and md_user.status=1';
        if(trim($username) != ''){
            $where .= ' and username like \'%' .$username . '%\'';
        }
        if($status != ''){
            $where .= ' and show_model_card='. $status;
        }
        $page = page('User', 10, $where, array('md_user.id' =>'desc'), 'md_user_model on md_user.id=md_user_model.uid', 'md_user.id, username, model_card, reg_time, md_user.status, show_model_card');
        $this->assign($page);
        $this->display();
    }
    
    public function checkModelCard($uid=0, $status=0){
        
        if(M('User_model')->where(array('uid' => $uid))->save(array('show_model_card' => $status))){
            if($status == 1){
                $msg = '麻豆卡审核通过，去首页看看效果吧';
            }else{
                $msg = '对不起，您的麻豆卡不符合要求，请重设';           
            }
            vendor('Api.ServerAPI');
            $p = new \ServerAPI(C('appKey'), C('appSecret'));
            $content = '{"content":"' .$msg. '","extra":"status'.$status.'"}';
            $to = array($uid);
            $msg = $p->messageSystemPublish(2, $to, 'RC:TxtMsg', $content);
            $this->ajaxReturn(array('code' => 1));
        }
        $this->ajaxReturn(array('code' => 0));
    }
    
    public function editModel(){
        if(IS_POST){
            $value = I('post.value');
            $id = I('post.id');
            M('User_model')->where(array('uid' => $id))->save(array('value' => $value));
            $this->redirect('Model/index');
        }else{
            $id = I('get.id');
            $user = M('User')->where(array('md_user.id' => $id))->join('md_user_model on md_user.id=md_user_model.uid')->find();
            $this->assign($user);
            $this->display();
        }
    }
}